<style media="screen">
    div{
      display: inline-block;
      width: 40%;
      float: left;
      padding: 10px;
      border: 1px black solid;
    }
    blockquote{
        margin-left: 10px;
        padding-left: 10px;
        border-left:3px black solid;
    }
</style>
<div id="old"></div>
<div id="new"></div>
<script src="/js/index.js" charset="utf-8"></script>
<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
  var letmd=new LetMDEngine();

  letmd.appendTextConfigs([
    {//添加对Header的匹配
      name:"header",
      type:1,
      check:function(arr,result){
        var text=arr[0].trim();
        if(/^#{1,6}\s+\S+\s*#*$/.test(text)){
          let level=text.match(/^#{1,6}/)[0].length
          let content=text.replace(/^#{1,6}\s+/,'').replace(/\s*#*$/,'');

          arr.shift();
          return {
            level,
            content
          };
        }
      },
      parse:function(data){
        return `<h${data.level}>${data.content}</h${data.level}>`
      }
    },
    {//添加对Header的匹配
      name:"header",
      type:1,
      check:function(arr,result){
        if(arr.length<2) return false;

        var text1=arr[0].trim();
        if(text1.length===0)return false;

        let level=0;
        var text2=arr[1].trim();
        if(/^={3,}$/.test(text2)){
            level=1;
        }

        if(/^-{3,}$/.test(text2)){
           level=2;
        }

        if(level){
          arr.shift();
          arr.shift();
          return {
            level:level,
            content:text1
          };
        }
      },
    },
    {//添加对分隔线的匹配
      name:"dividing-line",
      type:1,
      check:function(arr,result){
        var text=arr[0].split(/\s+/).join('');
        if(/^(-{3,}|\*{3,}|_{3,})$/.test(text)){
            arr.shift()
            return {};
        }
      },
      parse:function(data){
        return "<hr>";
      }
    },
    {//添加对区块引用的匹配
      name:"blockquote",
      type:1,
      check:function(arr,result){
        let intext=[];
        let i=0;
        for(i=0;i<arr.length;i++){
          let text=arr[i].trim();
          if(/^>/.test(text)){
            intext.push(text.substring(1));
          }else if(i>0&&/\s*/.test(text)&&arr[i+1]&&/^>/.test(arr[i+1].trim())){
            intext.push(text);
          }else{
            break;
          }
        }

        if(intext.length!==0){
            while(i>0){
              arr.shift();
              i--;
            }
            return {
              content:intext.join('\n')
            };
        }
      },
      parse:function(data){
        return `<blockquote>${letmd.parse(data.content)}</blockquote>`;
      }
    },
    {//添加对无序列表的匹配
      name:"unordered-list",
      type:1,
      check:function(arr,result){
        let lis=[];
        let li=[];
        let sign='';
        let i=0;
        for(i=0;i<arr.length;i++){
          let text=arr[i].trim();
          let text_notrim=arr[i];
          if(/^(\+|-|\*)\s/.test(text)&&(sign===''||text[0]===sign)){
            if(lis.length>0){
              li=[];
            }
            lis.push(li);
            sign=text[0];
            
            li.push(text.substring(2));
          }else if(i>0&&/(^\s{2,})|(^\s*$)/.test(text_notrim)){
            li.push(text);
          }else{
            break;
          }
        }

        if(lis.length!==0){
            
            while(i>0){
              arr.shift();
              i--;
            }
            
            let listData=[];
            lis.forEach((li)=>{
              listData.push(li.join('\n'))
            });
            
            return {
              listData:listData
            };
        }
      },
      parse:function(data){
        let HTML="";
        data.listData.forEach((list)=>{
          HTML+=`<li>${letmd.parse(list)}</li>`;
        });

        return `<ul>${HTML}</ul>`;
      }
    },
    {//添加对有序列表的匹配
      name:"ordered-list",
      type:1,
      check:function(arr,result){
        let lis=[];
        let li=[];
        let sign='';
        let i=0;
        for(i=0;i<arr.length;i++){
          let text=arr[i].trim();
          let text_notrim=arr[i];
          if(/^\d+\.\s/.test(text)){
            if(lis.length>0){
              li=[];
            }
            lis.push(li);
            sign=text[0];
            
            li.push(text.substring(3));
          }else if(i>0&&/(^\s{2,})|(^\s*$)/.test(text_notrim)){
            li.push(text);
          }else{
            break;
          }
        }

        if(lis.length!==0){
            let lisHTML=[];
            while(i>0){
              arr.shift();
              i--;
            }
            
            let listData=[];
            lis.forEach((li)=>{
              listData.push(li.join('\n'))
            });
            
            return {
              listData:listData
            };
        }
      },
      parse:function(data){
        let HTML="";
        data.listData.forEach((list)=>{
          HTML+=`<li>${letmd.parse(list)}</li>`;
        });

        return `<ol>${HTML}</ol>`;
      }
    },
    {//添加对代码区块-制表符格式的匹配
      name:"precode",
      type:1,
      check:function(arr,result){
        let codes=[];
        let i=0;
        for(i=0;i<arr.length;i++){
          let text=arr[i];
          if(/^(\s{4}|\t)/.test(text)){
            codes.push(text.replace(/^(\s{4}|\t)/,''));
          }else{
            break;
          }
        }

        if(codes.length!==0){
            while(i>0){
              arr.shift();
              i--;
            }

            return {
              content:codes.join('\n')
            }
        }
      },
    },
    {//添加对代码区块-夹层格式的匹配
      name:"precode",
      type:1,
      check:function(arr,result){
        let codes=[];
        let close=false;
        let i=0;
        for(i=0;i<arr.length;i++){
          let text=arr[i].trim();
          if(i===0&&!/^`{3}/.test(text)){
            break;
          }else if(i!==0&&/^`{3}\s*$/.test(text)){
            close=true;
            break;
          }
        }

        if(close){
            while(i>=0){
              codes.push(arr.shift())
              i--;
            }
            codes.pop();
            codes.shift(); 

            return {
              content:codes.join('\n')
            }
        }
      },
      parse:function(data){

        return `<pre><code>${data.content}</code></pre>`;
      }
    },
    {//添加对空行的匹配
      name:"null",
      type:1,
      check:function(arr,result){
        var text=arr[0];
        if(/^\s*$/.test(text)&&result[result.length-1].name!=="null"){
          arr.shift();
          return {};
        }
      },
      parse:function(data){

        return '';
      }
    },
    {//添加对p的匹配
      name:"text",
      type:1,
      check:function(arr,result){
        var text=arr[0].trim();
        arr.shift();
        let last=result.pop();
        if(last&&last.name==='text'){
          return {
            content:last.data.content+text
          };
        }else{
          return {
            content:text
          };
        }
        
      },
      parse:function(data){
        return `<p>${data.content}</p>`
      }
    }]); 

  $.get('/documents/example-1.md',function(data){
      $('#old').html(data);
      $('#new').html(letmd.parse(data));
    });

</script>